﻿using Clancy.Common.Extensions;
using Clancy.Interfaces;
using Clancy.Ui.Properties;
using ReactiveUI;
using System;
using System.Reactive.Linq;

namespace Clancy.Ui.ViewModels
{
    public class OperationModeViewModel : ClancyEditorViewModelBase
    {
        private readonly IOperationalAdapterProvider m_OperationalAdapterProvider;

        public OperationModeViewModel(IOperationalAdapterProvider operationalAdapterProvider, ISurveilanceStorage surveilanceStorage)
            : base(operationalAdapterProvider, surveilanceStorage)
        {
            m_OperationalAdapterProvider = operationalAdapterProvider;

            CreateOperationModeCommands();

            FillRibbon();
        }

        public ReactiveCommand<object> CmdOnlineMode { get; private set; }

        public ReactiveCommand<object> CmdStandAloneMode { get; private set; }

        protected override void DisposeElements()
        {
        }

        private void CreateOperationModeCommands()
        {
            CmdStandAloneMode = ReactiveCommand.Create(m_OperationalAdapterProvider.OperationMode.Select(mode => mode != OperationMode.StandAlone));
            CmdStandAloneMode.Subscribe(_ => m_OperationalAdapterProvider.SwitchOperationMode(OperationMode.StandAlone));
            DisposableRxElements.Add(CmdStandAloneMode);

            CmdOnlineMode = ReactiveCommand.Create(m_OperationalAdapterProvider.OperationMode.Select(mode => mode != OperationMode.Online));
            CmdOnlineMode.Subscribe(_ => m_OperationalAdapterProvider.SwitchOperationMode(OperationMode.Online));
            DisposableRxElements.Add(CmdStandAloneMode);
        }

        private void FillRibbon()
        {
            InsertRibbonButton(Resources.RibbonTab_Basic, Resources.RibbonGroup_OperationMode, Resources.RibbonElement_SwitchStandAlone, Resources.computer_3_32.ToBitmapImage(), CmdStandAloneMode);
            InsertRibbonButton(Resources.RibbonTab_Basic, Resources.RibbonGroup_OperationMode, Resources.RibbonElement_SwitchOnline, Resources.network_32.ToBitmapImage(), CmdOnlineMode);
        }
    }
}